package net.maku.biz.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import net.maku.biz.convert.BizMailboxConvert;
import net.maku.biz.entity.BizMailboxEntity;
import net.maku.biz.query.BizMailboxQuery;
import net.maku.biz.service.BizMailboxService;
import net.maku.biz.vo.BizMailboxVO;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.member.entity.MemberUserEntity;
import net.maku.member.service.MemberUserService;
import net.maku.system.entity.SysOrgEntity;
import net.maku.system.service.SysOrgService;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* 主席信箱
*
* @author 阿沐 babamu@126.com
* @since 1.0.0 2025-03-29
*/
@RestController
@RequestMapping("biz/biz_mailbox")
@Tag(name="主席信箱")
@AllArgsConstructor
public class BizMailboxController {
    private final BizMailboxService bizMailboxService;

    private final MemberUserService memberUserService;

    private final SysOrgService sysOrgService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('biz:biz_mailbox:page')")
    public Result<PageResult<BizMailboxVO>> page(@ParameterObject @Valid BizMailboxQuery query){
        PageResult<BizMailboxVO> page = bizMailboxService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('biz:biz_mailbox:info')")
    public Result<BizMailboxVO> get(@PathVariable("id") Long id){
        BizMailboxEntity entity = bizMailboxService.getById(id);
        BizMailboxVO mailboxVO = BizMailboxConvert.INSTANCE.convert(entity);
        MemberUserEntity memberUser = memberUserService.getById(entity.getMemberId());
        SysOrgEntity orgEntity = sysOrgService.getById(memberUser.getOrgId());
        mailboxVO.setMemberName(memberUser.getRealName());
        mailboxVO.setMobile(memberUser.getMobile());
        mailboxVO.setOrgName(orgEntity.getName());
        return Result.ok(mailboxVO);
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('biz:biz_mailbox:save')")
    public Result<String> save(@RequestBody BizMailboxVO vo){
        bizMailboxService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('biz:biz_mailbox:update')")
    public Result<String> update(@RequestBody @Valid BizMailboxVO vo){
        bizMailboxService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('biz:biz_mailbox:delete')")
    public Result<String> delete(@RequestBody List<Long> idList){
        bizMailboxService.delete(idList);

        return Result.ok();
    }
}